/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatGUI.java
 *
 * Created on 29/08/2011, 11:53:16 PM
 */
package messgui;

import java.rmi.RemoteException;
import messengerclient.Controller;
import messengerclient.observers.ObsGUI;
import messengercommon.dto.Message;
import messengercommon.person.Contact;
import messgui.classes.ChatList;
import messgui.classes.StatusBar;

/**
 *
 * @author Samus
 */
public class ChatGUI extends javax.swing.JFrame implements StatusBar, ObsGUI {
    
    private ChatList chatlist;
    private Contact contact;

    /** Creates new form ChatGUI */
    public ChatGUI(Contact contact) {
        this.chatlist = new ChatList();
        this.contact = contact;
        initComponents();
        Controller.getInstance().addObsList(contact.getUsername(), chatlist);
        this.Lbl_CName.setText("Estas hablando con " + contact.getNick() + " - " + contact.getStatus());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Lst_Chat = new javax.swing.JList();
        Lbl_CName = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        Txt_Message = new javax.swing.JTextPane();
        Btn_Send = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        Txt_Status = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        Lst_Chat.setModel(this.chatlist);
        Lst_Chat.setName("Lst_Chat"); // NOI18N
        jScrollPane1.setViewportView(Lst_Chat);

        Lbl_CName.setFont(new java.awt.Font("Bookman Old Style", 0, 18));
        Lbl_CName.setText("--------Contact Name--------");
        Lbl_CName.setName("Lbl_CName"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        Txt_Message.setName("Txt_Message"); // NOI18N
        jScrollPane2.setViewportView(Txt_Message);

        Btn_Send.setText("Send");
        Btn_Send.setName("Btn_Send"); // NOI18N
        Btn_Send.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Btn_SendMouseClicked(evt);
            }
        });

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        Txt_Status.setEnabled(false);
        Txt_Status.setName("Txt_Status"); // NOI18N
        jScrollPane3.setViewportView(Txt_Status);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 606, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(Btn_Send, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE))
                    .addComponent(Lbl_CName))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Lbl_CName)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addComponent(Btn_Send, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void Btn_SendMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Btn_SendMouseClicked
    Message message = new Message();
    message.setDestino(contact.getUsername());
    message.setOrigen(Controller.getInstance().getPersonName());
    message.setMensaje(Txt_Message.getText());
    try {
        sendMessage(message);
    } catch (RemoteException ex) {
        changeStatus("Ha ocurrido un error al enviar el mensaje");
    }
}//GEN-LAST:event_Btn_SendMouseClicked
    
private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    Controller.getInstance().removeObs(contact.getUsername());
}//GEN-LAST:event_formWindowClosed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new ChatGUI().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Btn_Send;
    private javax.swing.JLabel Lbl_CName;
    private javax.swing.JList Lst_Chat;
    private javax.swing.JTextPane Txt_Message;
    private javax.swing.JTextPane Txt_Status;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables

    public void changeStatus(String status) {
        Txt_Status.setText(status);
        Txt_Status.repaint();
    }
    
    public void receiveMessage(Message message) {
        chatlist.receiveMessage(message);
        Txt_Message.setText("");
    }
    
    public void statusChange(Contact contact) {
        if (!this.contact.getNick().equals(contact.getNick())) {
            changeStatus("El contacto cambió su nick a " + contact.getNick());
        } else if (!this.contact.getStatus().equals(contact.getStatus())) {
            changeStatus("El contacto cambió su estado a " + contact.getStatus());
        }
    }
    
    public void sendMessage(Message message) throws RemoteException {
        Controller.getInstance().sendMessage(message);
    }
}
